package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        member.setRegTime(new Date());
        memberDao.add(member);
    }

    //根据月份统计会员数量
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    //统计每月非会员人数
    @Override
    public List<Integer> findUnMemberCountByMonth(List<String> list) {
        List<Integer> list1 = new ArrayList<>();
        for(String m : list){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberUnCountBeforeDate(m);
            list1.add(count);
        }
        return list1;
    }




    //修改手机号,返回会员信息
    @Override
    public Member changePhoneNumber(Map map) {
        memberDao.changePhoneNumber(map);
        String telephone = (String) map.get("newPhoneNum");
        return memberDao.findByTelephone(telephone);
    }

    //修改会员信息
    @Override
    public Member update(Member member) {
        memberDao.update(member);
        return memberDao.findByTelephone(member.getPhoneNumber());
    }

    //批量删除会员信息
    @Override
    public void deleteMembersByIds(Integer[] params) {
        for (Integer id : params) {
            memberDao.deleteById(id);
        }
    }


    //根据生日统计会员年龄分布
    public List<Map<String, Object>> findByBirthdayCount() throws Exception {

        //获取会员生日
        List<Date> birthday=memberDao.findBirthday();
        //获取会员年龄
        List<Integer>  list=new ArrayList<>();
        int count1=0;//婴幼儿数量
        int count2=0;//少儿数量
        int count3=0;//青少年数量
        int count4=0;//青年数量
        int count5=0;//中年数量
        int count6=0;//老年数量
        int count7=0;//未填写
        for (Date date : birthday) {
            if(date != null){
                int age = DateUtils.getAgeByBirth(date);
                list.add(age);
            }else {
                count7++;
                continue;
            }
        }


        for (Integer i : list) {
            if (i>=0 && i<7){
                count1++;
            }
            if (i>=7&& i<13){
                count2++;
            }
            if (i>=13 &&i <18){
                count3++;
            }
            if (i>=18&& i<46){
                count4++;
            }
            if (i>=46&&i<67){
                count5++;
            }
            if(i>=67){
                count6++;
            }
        }
        Map<String,Object> m1=new HashMap<>();
        m1.put("name","婴幼儿");
        m1.put("value",count1);
        Map<String,Object> m2=new HashMap<>();
        m2.put("name","少儿");
        m2.put("value",count2);
        Map<String,Object> m3=new HashMap<>();
        m3.put("name","青少年");
        m3.put("value",count3);
        Map<String,Object> m4=new HashMap<>();
        m4.put("name","青年");
        m4.put("value",count4);
        Map<String,Object> m5=new HashMap<>();
        m5.put("name","中年");
        m5.put("value",count5);
        Map<String,Object> m6=new HashMap<>();
        m6.put("name","老年");
        m6.put("value",count6);
        Map<String,Object> m7=new HashMap<>();
        m7.put("name","年龄信息未知");
        m7.put("value",count7);


        List<Map<String,Object>> memberAgeCount = new ArrayList<>();
        if (!m1.get("value").equals(0) ){
            memberAgeCount.add(m1);
        }
        if (!m2.get("value").equals(0)){
            memberAgeCount.add(m2);
        }
        if (!m3.get("value").equals(0)){
            memberAgeCount.add(m3);
        }
        if (!m4.get("value").equals(0)){
            memberAgeCount.add(m4);
        }
        if (!m5.get("value").equals(0)){
            memberAgeCount.add(m5);
        }
        if (!m6.get("value").equals(0)){
            memberAgeCount.add(m6);
        }
        if (!m7.get("value").equals(0)){
            memberAgeCount.add(m7);
        }
        return memberAgeCount;
    }


    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询会员信息
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //编辑会员信息
    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    //根据id删除会员
    @Override
    public void delete(Integer id) {
        memberDao.deleteById(id);
    }

}
